/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.appspot.trafficando.service.aggregator;

import com.appspot.trafficando.domain.SortOrder;
import com.appspot.trafficando.domain.EventType;
import com.appspot.trafficando.util.GeoHelper;
import java.util.Comparator;
import org.apache.abdera.model.Entry;
import static com.appspot.trafficando.util.AtomHelper.getType;

/**
 *
 * @author Domenico Maria Giffone
 */
public class TypeAndDistanceSortOrder implements SortOrder {

    private TypeAndDistanceSortOrder() {
    }
    private static SortOrder instance = new TypeAndDistanceSortOrder();

    public static SortOrder getInstance() {
        return instance;
    }

    @Override
    public Comparator<Entry> getComparator() {
        return new Comparator<Entry>() {

            @Override
            public int compare(Entry e1, Entry e2) {
                EventType et1 = getType(e1), et2 = getType(e2);
                if (et1.equals(et2)) {
                        final Integer d1 = Integer.valueOf(GeoHelper.getDistance(e1));
                        final Integer d2 = Integer.valueOf(GeoHelper.getDistance(e2));
                        return d1.compareTo(d2);
                    } else {
                    return Integer.valueOf(et1.ordinal()).compareTo(et2.ordinal());
                }
            }
        };
    }
}
